package com.crm.Servlet;

import com.crm.Service.UserService;
import com.crm.ServiceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * @version : 1.0
 * @auther : hjx
 * @Date : 2021/6/26
 * @Description : 实训
 */
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String name = req.getParameter("txtName");
        String pwd = req.getParameter("txtPwd");

        System.out.println("name = " + name);
        System.out.println("pwd = " + pwd);
        UserService userService = new UserServiceImpl();
        if (name != null && pwd != null && userService.login(name,pwd)) {
            req.setAttribute("user",name);
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        } else {

            resp.sendRedirect(req.getContextPath() + "/login.jsp");
        }
    }
}
